/*
 * Inbox.java
 *
 * Created on May 15, 2008, 2:05 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package morseme;

/**
 *
 * @author tt
 */
import javax.wireless.messaging.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class Inbox extends List implements CommandListener{

private static final Command dispCmd = new Command("Display", Command.OK, 1);
private static final Command cancelCmd = new Command("Cancel", Command.CANCEL, 1);
private static Inbox inbox;

private Vector msgs = new Vector();
private MainMenu menu;
private MsgForm msgForm;

public Inbox(MainMenu menu) {

super("Inbox", List.EXCLUSIVE);

this.menu = menu;
msgForm = new MsgForm(this);

this.addCommand(dispCmd);
this.addCommand(cancelCmd);
this.setCommandListener(this);

updateInbox();

this.inbox = this;
}

public void updateInbox(){

// for(int i = 0; i < this.size(); i++){
// this.delete(i);
// }
this.deleteAll();
for(int i = 0; i < msgs.size(); i++){

Message msg = (Message)msgs.elementAt(i);
if(msg instanceof TextMessage){
TextMessage tmsg = (TextMessage) msg;
String st = tmsg.getPayloadText();
this.append(st, null);
}
}
}

public void commandAction(Command c, Displayable d) {

if(c == dispCmd){

if(this.msgForm != null && this.size() > 0){

int index = this.getSelectedIndex();
Message msg = (Message)msgs.elementAt(index);
String addr = msg.getAddress();
Date date = msg.getTimestamp();
TextMessage tm = (TextMessage)msg;
String detail = tm.getPayloadText();
this.msgForm.setMessage(addr, date, detail);

Display.getDisplay(MorseME.getMIDlet()).setCurrent(this.msgForm);
}

}else if(c == cancelCmd){

Display.getDisplay(MorseME.getMIDlet()).setCurrent(this.menu);
}
}

public static Inbox getInbox(){
if(inbox != null){
return inbox;
}
return null;
}

public void addMessage(Message msg){

if(!msgs.contains(msg)){

msgs.addElement(msg);
}
}
}

